package com.yzt.index;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.yzt.zhuju.MainMenuContsant;
import com.yzt.zhuju.arouter.RouterDynamic;
import com.yzt.zhuju.arouter.RouterMessage;
import com.yzt.zhuju.arouter.RouterMine;
import com.yzt.zhuju.arouter.RouterSayu;
import com.yzt.zhuju.file.FileManager;
import com.yzt.zhuju.network.entity.NavIconEntity;
import com.yzt.index.bean.MainInfo;
import com.yzt.zhuju.constants.PreferenceConstant;
import com.yzt.p_base.utils.SharedPreferenceUtil;
import com.yzt.plugin_widget.tableBar.bean.TabBean;
import com.yzt.plugin_widget.tableBar.listener.CustomTabEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainMenuHelper {

    /**
     * 获取主页展示的tab信息和fragment信息
     */
    public static MainInfo getMainMenuInfo() {
        MainInfo mainInfo = new MainInfo();
        for (String menu : MainMenuContsant.getListMenu()) {
            switch (menu) {
                //交友
                case MainMenuContsant.MAIN_MENU_NEARBY:
                    mainInfo.getCustomTabList().add(new TabBean(
                            "",
                            R.mipmap.tab_jiaoyou,
                            R.mipmap.tab_jiaoyou_selected));
                    mainInfo.getFragmentList().add(RouterSayu.indexFragment());
                    break;
                //直播
                case MainMenuContsant.MAIN_MENU_CHATROOM:
                    mainInfo.getCustomTabList().add(new TabBean(
                            "",
                            R.mipmap.tab_home,
                            R.mipmap.tab_home_selected));
                    mainInfo.getFragmentList().add(RouterDynamic.GameListFragment());
                    break;

                //消息
                case MainMenuContsant.MAIN_MENU_MSG:
                    mainInfo.getCustomTabList().add(new TabBean(
                            "",
                            R.mipmap.tab_message,
                            R.mipmap.tab_message_selected));
                    mainInfo.getFragmentList().add(RouterMessage.MessageFatherFragment());
                    break;
                //我的
                case MainMenuContsant.MAIN_MENU_MINE:
                    mainInfo.getCustomTabList().add(new TabBean(
                            "",
                            R.mipmap.tab_me,
                            R.mipmap.tab_me_selected));
                    mainInfo.getFragmentList().add(RouterMine.MineFragment());
                    break;
                default:
                    break;
            }
        }

        //判断icon本地,是否存在icon数据，没有的话就使用默认，有的话判断icon图片是否存在
        String iconJson = SharedPreferenceUtil.getInstance().getValue(PreferenceConstant.MAIN_TAB_ICON, "");
        if (!TextUtils.isEmpty(iconJson)) {
            try {
                NavIconEntity.Icon icons = JSON.parseObject(iconJson, NavIconEntity.Icon.class);
                ArrayList<CustomTabEntity> customTabEntities = mainInfo.getCustomTabList();
                if (customTabEntities != null && icons.tableIcon != null && icons.tableIcon.size() > 0) {
                    List<HashMap<String, String>> mapList = icons.tableIcon;
                    for (int i = 0, len = mapList.size(); i < len; i++) {

                        if (customTabEntities.size() >= i) {
                            Map<String, String> map = mapList.get(i);
                            String unchecked = FileManager.getDefaultPath() + File.separator + "icons" + File.separator + map.get("unchecked");
                            String selected = FileManager.getDefaultPath() + File.separator + "icons" + File.separator + map.get("selected");
                            customTabEntities.get(i).setTabSelectedIcon4String(selected);
                            customTabEntities.get(i).getTabUnselectedIcon4String(unchecked);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mainInfo;
    }
}
